//
//  ViewController.m
//  小文件下载
//
//  Created by 李煜 on 2024/3/5.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self download1];
//    [self download2];
    [self download3];
}
- (void)download1 {

    NSString *urlStr = [@"https://t7.baidu.com/it/u=1819248061,230866778&fm=193&f=GIF" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr] ;
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    self.imageView.image = image;
}

- (void)download2 {
    NSString *urlStr = [@"https://t7.baidu.com/it/u=1819248061,230866778&fm=193&f=GIF" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr] ;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        self.imageView.image = image;
    }];
}
- (void)download3 {
//    https://ak.picdn.net/shutterstock/videos/1054532051/preview/stock-footage-happy-young-married-couple-walks-with-two-dogs-in-a-glade-in-the-park-girl-with-her-boyfriend.mp4

//https://download.motionplaces.com/motionplaces.com-0048-c018.mp4
    
    NSString *urlStr = [@"https://v3.cdnpk.net/videvo_files/video/free/2014-12/large_preview/Raindrops_Videvo.mp4" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr] ;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc]initWithRequest:request delegate:self];
    NSURLSession *session = [NSURLSession sharedSession];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"---fail error : %@",error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"---connectionDidFinishLoading");

}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}
@end
